{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Reverse Only Letters"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #two-pointers #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #双指针 #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: reverseOnlyLetters"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #仅仅反转字母"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个字符串 <code>s</code> ，根据下述规则反转字符串：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>所有非英文字母保留在原有位置。</li>\n",
    "\t<li>所有英文字母（小写或大写）位置反转。</li>\n",
    "</ul>\n",
    "\n",
    "<p>返回反转后的 <code>s</code><em> 。</em></p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<ol>\n",
    "</ol>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>s = \"ab-cd\"\n",
    "<strong>输出：</strong>\"dc-ba\"\n",
    "</pre>\n",
    "\n",
    "<ol>\n",
    "</ol>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>s = \"a-bC-dEf-ghIj\"\n",
    "<strong>输出：</strong>\"j-Ih-gfE-dCba\"\n",
    "</pre>\n",
    "\n",
    "<ol>\n",
    "</ol>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>s = \"Test1ng-Leet=code-Q!\"\n",
    "<strong>输出：</strong>\"Qedo1ct-eeLg=ntse-T!\"\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= s.length &lt;= 100</code></li>\n",
    "\t<li><code>s</code> 仅由 ASCII 值在范围 <code>[33, 122]</code> 的字符组成</li>\n",
    "\t<li><code>s</code> 不含 <code>'\\\"'</code> 或 <code>'\\\\'</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [reverse-only-letters](https://leetcode.cn/problems/reverse-only-letters/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [reverse-only-letters](https://leetcode.cn/problems/reverse-only-letters/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"ab-cd\"', '\"a-bC-dEf-ghIj\"', '\"Test1ng-Leet=code-Q!\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseOnlyLetters(self, s: str) -> str:\n",
    "        letters = [c for c in s if c.isalpha()]\n",
    "        ans = []\n",
    "        for i in s:\n",
    "            if i.isalpha():\n",
    "                ans.append(letters.pop())\n",
    "            else:\n",
    "                ans.append(i)\n",
    "        return \"\".join(ans)\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pd(self, c):\n",
    "        _c = ord(c)\n",
    "        a = ord(\"a\")\n",
    "        z = ord(\"z\")\n",
    "        A = ord(\"A\")\n",
    "        Z = ord(\"Z\")\n",
    "        if (_c >= a and _c <= z) or (_c >= A and _c <= Z):\n",
    "            return True\n",
    "        return False\n",
    "        \n",
    "    def reverseOnlyLetters(self, S):\n",
    "        \"\"\"\n",
    "        :type S: str\n",
    "        :rtype: str\n",
    "        \"\"\"\n",
    "        k = []\n",
    "        S = list(S)\n",
    "        for i in range(0,len(S)):\n",
    "            if self.pd(S[i]):\n",
    "                k.append(S[i])\n",
    "        s = \"\"\n",
    "        for i in range(0,len(S)):\n",
    "            if self.pd(S[i]):\n",
    "                S[i] = k.pop()\n",
    "            s += S[i]\n",
    "        return s\n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def check_letter(self,s):\n",
    "        if (s>=\"A\" and s<=\"Z\") or (s>=\"a\" and s<=\"z\"):\n",
    "            return True\n",
    "        else:\n",
    "            return False\n",
    "    def reverseOnlyLetters(self, S):\n",
    "        \"\"\"\n",
    "        :type S: str\n",
    "        :rtype: str\n",
    "        \"\"\"\n",
    "        list_str=list(S)\n",
    "        N=len(list_str)\n",
    "        if N==0 or N==1:\n",
    "            return S\n",
    "        i=0\n",
    "        j=N-1\n",
    "        while i<j:\n",
    "            while i<N and not self.check_letter(list_str[i]):\n",
    "                i+=1\n",
    "            while j>=0 and not self.check_letter(list_str[j]):\n",
    "                j-=1\n",
    "            if i>=j:\n",
    "                break\n",
    "            temp=list_str[i]\n",
    "            list_str[i]=list_str[j]\n",
    "            list_str[j]=temp\n",
    "            i+=1\n",
    "            j-=1\n",
    "        return \"\".join(list_str)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseOnlyLetters(self, S):\n",
    "        \"\"\"\n",
    "        :type S: str\n",
    "        :rtype: str\n",
    "        \"\"\"\n",
    "        list1 = list(S)\n",
    "        self.char_addr = {}\n",
    "        for each in list1:\n",
    "                if each.isalpha() == False:\n",
    "                    self.char_addr[list1.index(each)]=each\n",
    "                    print()\n",
    "                    list1[list1.index(each)] = ''\n",
    "        list1 = list(''.join(reversed(list1)))\n",
    "        for k,v in self.char_addr.items():\n",
    "            list1.insert(k,v)\n",
    "        return ''.join(list1)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseOnlyLetters(self, S):\n",
    "        \"\"\"\n",
    "        :type S: str\n",
    "        :rtype: str\n",
    "        \"\"\"\n",
    "        m= 0\n",
    "        start = 0\n",
    "        l = len(S)\n",
    "        end = l -1\n",
    "        \n",
    "        S= list(S)\n",
    "       \n",
    "        while(start < end):\n",
    "            \n",
    "            while(not S[start].isalpha()): \n",
    "                start += 1\n",
    "                if start == l -1 : break\n",
    "            while(not S[end].isalpha()): \n",
    "                end -= 1\n",
    "                if end == 0: break\n",
    "                \n",
    "            if start >= end : return ''.join(S)\n",
    "            S[start],S[end] = S[end],S[start]\n",
    "            start += 1\n",
    "            end -=1\n",
    "        return ''.join(S)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseOnlyLetters(self, S):\n",
    "        \"\"\"\n",
    "        :type S: str\n",
    "        :rtype: str\n",
    "        \"\"\"\n",
    "        temp = 'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM'\n",
    "        temp_num = []\n",
    "        temp_str = []\n",
    "        temp_a = []\n",
    "        result = \"\"\n",
    "        for i,s in enumerate(S):\n",
    "            if s not in temp:\n",
    "                temp_num.append(i)\n",
    "                temp_str.append(s)\n",
    "                continue\n",
    "            temp_a.append(s)\n",
    "        print(temp_a)\n",
    "        print(temp_num)\n",
    "        print(temp_str)\n",
    "        j = 0\n",
    "        k = 0\n",
    "        temp_a = temp_a[::-1]\n",
    "        for i in range(len(S)):\n",
    "            if i in temp_num:\n",
    "                result += temp_str[j]\n",
    "                j+=1\n",
    "                continue\n",
    "            result += temp_a[k]\n",
    "            k+=1\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseOnlyLetters(self, S):\n",
    "        \"\"\"\n",
    "        :type S: str\n",
    "        :rtype: str\n",
    "        \"\"\"\n",
    "        S = list(S)\n",
    "        s = list(filter(self.get_characters,S))\n",
    "        print(s)\n",
    "#        s.reverse()\n",
    "      \n",
    "        for i in range(len(S)):\n",
    "            if (ord(S[i])>64 and ord(S[i])<91) or (ord(S[i])>96 and ord(S[i])<123) :\n",
    "                S[i] = s.pop()\n",
    "        return \"\".join(S)\n",
    "                \n",
    "        \n",
    "\n",
    "    def get_characters(self,s):\n",
    "        \n",
    "        ord_i = ord(s)\n",
    "        print(s)\n",
    "        print(ord_i)\n",
    "        return (ord_i>64 and ord_i<91) or (ord_i>96 and ord_i<123)\n",
    "               \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseOnlyLetters(self, S):\n",
    "        \"\"\"\n",
    "        :type S: str\n",
    "        :rtype: str\n",
    "        \"\"\"\n",
    "        j = -1\n",
    "        ans = [\"\" for _ in range(len(S))]\n",
    "        for i in range(len(S)):\n",
    "            if S[i].isalpha():\n",
    "                while not S[j].isalpha():\n",
    "                    j = j - 1\n",
    "                ans[i] = S[j]\n",
    "                j = j - 1\n",
    "            else:\n",
    "                ans[i] = S[i]\n",
    "        return \"\".join(ans)\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from string import ascii_letters\n",
    "class Solution:\n",
    "    def reverseOnlyLetters(self, S):\n",
    "        \"\"\"\n",
    "        :type S: str\n",
    "        :rtype: str\n",
    "        \"\"\"\n",
    "        b=[]\n",
    "        for v in S:\n",
    "            if v in ascii_letters:\n",
    "                b.append(v)\n",
    "        b.reverse()\n",
    "        #print(b)\n",
    "        for i,v in enumerate(S):\n",
    "            if v not in ascii_letters:\n",
    "                b.insert(i,v)\n",
    "        return ''.join(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseOnlyLetters(self, S: str) -> str:\n",
    "        i, j = 0, len(S)-1\n",
    "        S = list(S)\n",
    "        while i < j:\n",
    "            if S[i].isalpha() and S[j].isalpha():\n",
    "                S[i], S[j] = S[j], S[i]\n",
    "                i, j = i + 1, j - 1\n",
    "            i += 1 if not S[i].isalpha() else 0\n",
    "            j -= 1 if not S[j].isalpha() else 0\n",
    "        return ''.join(S)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseOnlyLetters(self, S: str) -> str:\n",
    "        if S.isalpha()==True:\n",
    "            return S[::-1]\n",
    "        marks=[]\n",
    "        lst=[]\n",
    "        for i in range(len(S)):\n",
    "            if S[i].isalpha()==True:\n",
    "                lst.insert(0,S[i])\n",
    "            else:\n",
    "                marks.append((S[i],i))\n",
    "                \n",
    "        for a,b in marks:\n",
    "            lst.insert(b,a)\n",
    "        return ''.join(lst)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseOnlyLetters(self, S: str) -> str:\n",
    "        slist = list(S)\n",
    "        n = len(slist)\n",
    "        left = 0\n",
    "        right = n-1\n",
    "\n",
    "        while left < right:\n",
    "            if slist[left].isalpha() and slist[right].isalpha():\n",
    "                slist[left],slist[right]=slist[right],slist[left]\n",
    "                left += 1\n",
    "                right -= 1\n",
    "            elif slist[left].isalpha() and not slist[right].isalpha():\n",
    "                right -= 1\n",
    "            elif not slist[left].isalpha() and slist[right].isalpha():\n",
    "                left += 1\n",
    "            elif not slist[left].isalpha() and not slist[right].isalpha():\n",
    "                left += 1\n",
    "                right -= 1\n",
    "\n",
    "        return ''.join(slist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseOnlyLetters(self, S: str) -> str:\n",
    "        start = 0\n",
    "        end = len(S)-1\n",
    "        s1, s2 = \"\", \"\"\n",
    "        while start <= end:\n",
    "            if start == end:\n",
    "                s1+=S[start]\n",
    "                break\n",
    "            if not S[start].isalpha():\n",
    "                s1 += S[start]\n",
    "                start += 1\n",
    "                continue\n",
    "            if not S[end].isalpha():\n",
    "                s2 = S[end] + s2\n",
    "                end -= 1\n",
    "                continue\n",
    "            s1 += S[end]\n",
    "            s2 = S[start] + s2\n",
    "            end -= 1\n",
    "            start += 1\n",
    "        return s1+s2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseOnlyLetters(self, S: str) -> str:\n",
    "        if not S:\n",
    "            return ''\n",
    "        letters = [c for c in S if c.isalpha()]\n",
    "        ans = []\n",
    "        for c in S:\n",
    "            if c.isalpha():\n",
    "                ans.append(letters.pop())\n",
    "            else:\n",
    "                ans.append(c)\n",
    "        return \"\".join(ans)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseOnlyLetters(self, S: str) -> str:\n",
    "        left = 0\n",
    "        right = len(S)-1\n",
    "        S = list(S)\n",
    "        while left < right:\n",
    "            if ord(S[left]) not in range(65, 91) and ord(S[left]) not in range(97, 123):\n",
    "                left = left + 1\n",
    "            elif ord(S[right]) not in range(65, 91) and ord(S[right]) not in range(97, 123):\n",
    "                right = right - 1\n",
    "            else:\n",
    "                S[left], S[right] = S[right], S[left]\n",
    "                left = left + 1\n",
    "                right = right - 1\n",
    "        return ''.join(S)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseOnlyLetters(self, S: str) -> str:\n",
    "        res = []\n",
    "        s = list(S)\n",
    "        j = len(s) - 1\n",
    "        while j >= 0:\n",
    "            if (s[j] <= 'z' and s[j] >= 'a') or (s[j] <= 'Z' and s[j] >= 'A'):\n",
    "                res.append(s[j])\n",
    "            j -= 1\n",
    "        i = 0\n",
    "        while i < len(s):\n",
    "            if s[i] < 'A' or s[i] > 'z' or (s[i] > 'Z' and s[i] < 'a'):\n",
    "                res.insert(i, s[i])\n",
    "            i +=1\n",
    "        res = ''.join(res)\n",
    "        return res\n",
    "\n",
    "        \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseOnlyLetters(self, S: str) -> str:\n",
    "        St=[]\n",
    "        for i in range(len(S)):\n",
    "            if S[i].isalpha():\n",
    "                St.append(S[i])\n",
    "        print(St)\n",
    "        St.reverse()\n",
    "        print(St)\n",
    "        for j in range(len(S)):\n",
    "            if S[j].isalpha()==False:\n",
    "                St.insert(j,S[j])\n",
    "        print(St)\n",
    "        s=''.join(St)\n",
    "        return s\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseOnlyLetters(self, S: str) -> str:\n",
    "        letters = [c for c in S if c.isalpha()]\n",
    "        ans = []\n",
    "        for c in S:\n",
    "            if c.isalpha():\n",
    "                ans.append(letters.pop())\n",
    "            else:\n",
    "                ans.append(c)\n",
    "        return \"\".join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isA(self,c):\n",
    "        print(c)\n",
    "        if ord(c) >= ord('a') and ord(c) <= ord('z'):\n",
    "            return True\n",
    "        if ord(c) >= ord('A') and ord(c) <= ord('Z'):\n",
    "            return True\n",
    "        return False\n",
    "    def reverseOnlyLetters(self, S: str) -> str:\n",
    "        z = list(S)\n",
    "        r = list(S)\n",
    "        index = 0\n",
    "        for i in range(len(z)-1,-1,-1):\n",
    "            if self.isA(z[i]) == True:\n",
    "                while self.isA(r[index]) == False and index < len(r):\n",
    "                    index += 1\n",
    "                r[index] = z[i]\n",
    "                index += 1\n",
    "        ska = \"\"\n",
    "        return ska.join(r)\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isApha(self, s):\n",
    "        return bool('a' <= s <= 'z' or 'A' <= s <= 'Z')\n",
    "\n",
    "    def reverseOnlyLetters(self, S: str) -> str:\n",
    "        listS = list(S)\n",
    "        lenS = len(S)\n",
    "        left, right = 0, lenS - 1\n",
    "\n",
    "        while left < right:\n",
    "            while left < right and not self.isApha(listS[left]):\n",
    "                left += 1\n",
    "            while left < right and not self.isApha(listS[right]):\n",
    "                right -= 1\n",
    "            if left >= right:\n",
    "                break\n",
    "            listS[left], listS[right] = listS[right], listS[left]\n",
    "            left += 1\n",
    "            right -= 1\n",
    "        return \"\".join(listS)\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseOnlyLetters(self, s: str) -> str:\n",
    "        i=0\n",
    "        j=len(s)-1\n",
    "        start=''\n",
    "        last=''\n",
    "        while i<=j:\n",
    "            while i<=j and not s[i].isalpha():\n",
    "                start+=s[i]\n",
    "                i+=1\n",
    "            while i<=j and not s[j].isalpha():\n",
    "                last=s[j]+last\n",
    "                j-=1\n",
    "            if i<j:\n",
    "                start+=s[j]\n",
    "                last=s[i]+last\n",
    "            else:\n",
    "                if i==j:\n",
    "                    start+=s[j]\n",
    "            i+=1\n",
    "            j-=1\n",
    "        # print(start)\n",
    "        # print(last)\n",
    "        return start+last\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseOnlyLetters(self, s: str) -> str:\n",
    "        # 双指针\n",
    "        res = list(s)\n",
    "        left = 0\n",
    "        right = len(s) - 1\n",
    "        while left < right:\n",
    "            while left <right and not res[left].isalpha():\n",
    "                left += 1\n",
    "            while right > left and not res[right].isalpha():\n",
    "                right -= 1\n",
    "            if left < right:\n",
    "                res[left], res[right] = res[right], res[left]\n",
    "            left += 1\n",
    "            right -= 1\n",
    "        return ''.join(res)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseOnlyLetters(self, s: str) -> str:\n",
    "        i, j = 0, len(s)-1\n",
    "        ascii = [i for i in range(65,91)]\n",
    "        ascii.extend([i for i in range(97,123)])\n",
    "        while i < j:\n",
    "            if ord(s[i]) not in ascii: #i不是字母，i+1\n",
    "                i += 1\n",
    "            elif ord(s[j]) not in ascii: #j不是字母，j-1\n",
    "                j -= 1\n",
    "            else:#两个都是字母，交换位置\n",
    "                s = s[:i] + s[j] + s [i+1:j] + s[i] + s[j+1:]\n",
    "                i += 1\n",
    "                j -= 1\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseOnlyLetters(self, s: str) -> str:\n",
    "        # 双指针\n",
    "        n = len(s)\n",
    "        l, r = 0, n-1\n",
    "        res = [''] * n\n",
    "        while l <= r:\n",
    "            while l < r and not s[l].isalpha():\n",
    "                res[l] = s[l]\n",
    "                l += 1\n",
    "            while l < r and not s[r].isalpha():\n",
    "                res[r] = s[r]\n",
    "                r -= 1\n",
    "            if l > r: break\n",
    "            print(l,r)\n",
    "            res[l], res[r] = s[r], s[l]\n",
    "            l, r = l+1, r-1\n",
    "\n",
    "        return ''.join(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from curses.ascii import isalpha\r\n",
    "\r\n",
    "\r\n",
    "class Solution:\r\n",
    "    def reverseOnlyLetters(self, s: str) -> str:\r\n",
    "        ans = \"\"\r\n",
    "        l = len(s)-1\r\n",
    "        for i in range(len(s)):\r\n",
    "            if s[i].isalpha():\r\n",
    "                while l >= 0 and not s[l].isalpha(): l -= 1\r\n",
    "                ans += s[l]\r\n",
    "                l -= 1 \r\n",
    "            else:\r\n",
    "                ans += s[i]\r\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseOnlyLetters(self, S):\n",
    "        \"\"\"\n",
    "        :type S: str\n",
    "        :rtype: str\n",
    "        \"\"\"\n",
    "        p = [i for i in S if i.isalpha()]\n",
    "        return ''.join([i if not i.isalpha() else p.pop() for i in S])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def reverseOnlyLetters(self, s: str) -> str:\r\n",
    "        ret=list(s)\r\n",
    "        l,r=0,len(s)-1\r\n",
    "        while l<r:\r\n",
    "            while l<r and not s[l].isalpha():\r\n",
    "                l+=1\r\n",
    "            while l<r and not s[r].isalpha():\r\n",
    "                r-=1\r\n",
    "            ret[l],ret[r]=ret[r],ret[l]\r\n",
    "            l+=1\r\n",
    "            r-=1\r\n",
    "        return ''.join(ret)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseOnlyLetters(self, s: str) -> str:\n",
    "        i,j=0,len(s)-1\n",
    "        lst=[*s]\n",
    "        while i<j:\n",
    "            if lst[i].isalpha() and lst[j].isalpha():\n",
    "                lst[i],lst[j]=lst[j],lst[i]\n",
    "                i+=1\n",
    "                j-=1\n",
    "            elif not lst[i].isalpha():\n",
    "                i+=1\n",
    "            elif not lst[j].isalpha():\n",
    "                j-=1\n",
    "        return ''.join(lst)\n",
    "\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
